#!/bin/bash

# Windows环境自动化部署脚本
SERVER_IP="106.75.5.247"
USERNAME="ubuntu"
PASSWORD="we13599793044"

echo "🚀 开始Windows环境自动化部署..."

# 检查expect是否可用，如果没有就尝试安装
if ! command -v expect &> /dev/null; then
    echo "⚠️  正在安装expect..."
    # 在Git Bash中可能需要手动安装expect
    echo "请确保已安装expect，如果没有请运行: pacman -S expect"
fi

# 上传WAR文件
echo "📤 上传应用文件..."
expect << EOF
set timeout 30
spawn scp -o StrictHostKeyChecking=no target/OrderStatsSystem-1.0-SNAPSHOT.war $USERNAME@$SERVER_IP:/tmp/app.war
expect {
    "password:" {
        send "$PASSWORD\r"
        exp_continue
    }
    "100%" {
        puts "文件上传完成"
    }
    timeout {
        puts "上传超时"
        exit 1
    }
}
expect eof
EOF

if [ $? -ne 0 ]; then
    echo "❌ 文件上传失败"
    exit 1
fi

echo "🔧 执行远程部署..."

# 执行远程部署命令
expect << EOF
set timeout 300
spawn ssh -o StrictHostKeyChecking=no $USERNAME@$SERVER_IP
expect "password:"
send "$PASSWORD\r"

expect "$ "
send "echo '📦 更新系统并安装软件...'\r"
expect "$ "
send "sudo apt update -y\r"
expect {
    "password" {
        send "$PASSWORD\r"
        exp_continue
    }
    "$ " {
        send "sudo DEBIAN_FRONTEND=noninteractive apt install -y openjdk-11-jdk tomcat9 tomcat9-admin nginx wget curl unzip sqlite3\r"
    }
}

expect "$ "
send "echo '☕ 配置Java环境...'\r"
expect "$ "
send "sudo update-alternatives --set java /usr/lib/jvm/java-11-openjdk-amd64/bin/java\r"

expect "$ "
send "echo '🛑 停止现有服务...'\r"
expect "$ "
send "sudo systemctl stop tomcat9 2>/dev/null || true\r"
expect "$ "
send "sudo systemctl stop nginx 2>/dev/null || true\r"

expect "$ "
send "echo '🚀 部署应用...'\r"
expect "$ "
send "sudo rm -rf /var/lib/tomcat9/webapps/ROOT*\r"
expect "$ "
send "sudo cp /tmp/app.war /var/lib/tomcat9/webapps/ROOT.war\r"
expect "$ "
send "sudo chown tomcat:tomcat /var/lib/tomcat9/webapps/ROOT.war\r"

expect "$ "
send "echo '⚙️ 配置Tomcat...'\r"
expect "$ "
send "echo 'export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64' | sudo tee /etc/default/tomcat9\r"
expect "$ "
send "echo 'export CATALINA_OPTS=\"-Xmx2048m -Xms1024m -XX:+UseG1GC\"' | sudo tee -a /etc/default/tomcat9\r"

expect "$ "
send "echo '🌐 配置Nginx...'\r"
expect "$ "
send "sudo tee /etc/nginx/sites-available/default > /dev/null << 'NGINXEOF'\r"
send "server {\r"
send "    listen 80 default_server;\r"
send "    listen \[::]:80 default_server;\r"
send "    server_name _;\r"
send "    location / {\r"
send "        proxy_pass http://localhost:8080;\r"
send "        proxy_set_header Host \\\$host;\r"
send "        proxy_set_header X-Real-IP \\\$remote_addr;\r"
send "        proxy_set_header X-Forwarded-For \\\$proxy_add_x_forwarded_for;\r"
send "        proxy_set_header X-Forwarded-Proto \\\$scheme;\r"
send "    }\r"
send "}\r"
send "NGINXEOF\r"

expect "$ "
send "echo '🔥 配置防火墙...'\r"
expect "$ "
send "sudo ufw allow OpenSSH 2>/dev/null || true\r"
expect "$ "
send "sudo ufw allow 80/tcp 2>/dev/null || true\r"
expect "$ "
send "sudo ufw allow 8080/tcp 2>/dev/null || true\r"
expect "$ "
send "sudo ufw --force enable 2>/dev/null || true\r"

expect "$ "
send "echo '🔧 启动服务...'\r"
expect "$ "
send "sudo systemctl start tomcat9\r"
expect "$ "
send "sudo systemctl enable tomcat9\r"
expect "$ "
send "sudo systemctl start nginx\r"
expect "$ "
send "sudo systemctl enable nginx\r"

expect "$ "
send "echo '⏳ 等待服务启动...'\r"
expect "$ "
send "sleep 30\r"

expect "$ "
send "echo '🔍 检查服务状态...'\r"
expect "$ "
send "sudo systemctl is-active tomcat9\r"
expect "$ "
send "sudo systemctl is-active nginx\r"

expect "$ "
send "echo '🎯 测试访问...'\r"
expect "$ "
send "curl -I http://localhost:8080 2>/dev/null || echo 'Tomcat未就绪'\r"
expect "$ "
send "curl -I http://localhost 2>/dev/null || echo 'Nginx未就绪'\r"

expect "$ "
send "echo '✅ 部署完成！'\r"
expect "$ "
send "echo '📱 访问地址: http://$SERVER_IP'\r"
expect "$ "
send "echo '🔐 默认账号: admin / admin123'\r"

expect "$ "
send "exit\r"
expect eof
EOF

echo ""
echo "🎉 自动化部署完成！"
echo "📱 请访问: http://$SERVER_IP"
echo "🔐 默认账号: admin / admin123"
echo ""
echo "📊 如需查看日志："
echo "   ssh $USERNAME@$SERVER_IP"
echo "   sudo journalctl -u tomcat9 -f" 